package com.security.door.controller.accessManage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.baseRole;
import com.security.door.model.domain.baseUsers;
import com.security.door.model.dto.baseRoleDto;
import com.security.door.model.dto.baseUsersDto;
import com.security.door.service.IbaseRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - baseRoleController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class baseRoleController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IbaseRoleService ibaseRoleService;

    @PostMapping("/role/queryListWithPage")
    @ApiOperation(value = "分页查询所有角色信息", notes = "分页查询所有角色信息")
    public Wrapper queryListWithPage(@RequestBody baseRoleDto entityDto) {
        var list = ibaseRoleService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping(value = "/role/delRole/{roleCode}")
    public Wrapper DelRole(@PathVariable("roleCode") int roleCode) {
        if (ibaseRoleService.checkCode(roleCode) > 0)
            return WrapMapper.error("角色已经被使用,不允许删除");
        int result = ibaseRoleService.deleteByRoleCode(roleCode);
        String resultStr = result == 0 ? "数据不存在" : "操作成功";
        return WrapMapper.ok(resultStr);
    }

    @GetMapping(value = "/role/checkCode/{roleCode}")
    public int checkCode(@PathVariable("roleCode") Integer roleCode) {
        int data = ibaseRoleService.checkCode(roleCode);
        return data;
    }

    @PostMapping(value = "/role/addBaseRole")
    public Wrapper addBaseRole(@RequestBody baseRole entity) {
        if (null != entity) {
            if (null == entity.getGuid() || "".equals(entity.getGuid())) {
                entity.setGuid(UUID.randomUUID().toString());
                entity.setRolecode(ibaseRoleService.getMaxID());
                entity.setCreatetime(new Date());
                ibaseRoleService.save(entity);
            } else {
                ibaseRoleService.update(entity);
            }
        }
        return WrapMapper.ok("操作成功");
    }
}
